package com.sendi.system.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONObject;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;

import com.sendi.system.entity.Dctdata;
import com.sendi.system.entity.SysPushConfig;

public class PushConfigAction extends SysAction {
	
	private Integer id;
	private String callbackfun;
	private String callbackmoudleid;
	private String description;
	private String pushid;
 
	  
	public PushConfigAction()
	{
		super();
		data_session_name="PushConfigActionData";
	}
	
	
	public void PushConfigInit()
	{
		List datas=null;
		
		if(getSession().getAttribute(data_session_name)!=null)
		{
			datas=(List)getSession().getAttribute(data_session_name);
			System.out.println("use session size"+datas.size());
		}
		else
		{
			addPushConfigToSesson();
			datas=(List)getSession().getAttribute(data_session_name);
		}
	 
			writeJSONArray(datas);
		 
	}
	
	public void addPushConfigToSesson()
	{
		List datas=null;
		String hql="from  SysPushConfig ";
	 	datas=serviceSupportImp.findByHQL(hql);
	 	getSession().setAttribute(data_session_name,datas);
	 	System.out.println("data size:"+datas.size());
	}
	
	public void queryPushConfigData()
	{
		   try
		   {
			     Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
			 	StringBuffer hql=new StringBuffer("from SysPushConfig where 1=1 ");
				if(this.description!=null&&description.trim().length()>0)
				{
					hql.append(" and  description like :description ");
				}
				if(this.pushid!=null&&pushid.trim().length()>0)
				{
					hql.append(" and  pushid like :pushid ");
				}
			 	
				   Query query= session.createQuery(hql.toString());
				   if(description!=null&&description.trim().length()>0)
					{
					  query.setParameter("description", "%"+description+"%");
					}
				   if(pushid!=null&&pushid.trim().length()>0)
					{
					  query.setParameter("pushid", "%"+pushid+"%");
					}
					 
					    List datas=query.list();
					    System.out.println("query size"+datas.size());
				     	getSession().setAttribute(data_session_name,datas);	
				     	writeResponseText("{success:true,msg:'success',datasize:"+datas.size()+"}");
		   			}
		   catch (Exception e) {
			   writeResponseText("{success:false,msg:'failure'}");
				 
		}
		    
		
	}
 
 
	
	 public void addPushConfig()
	 {
		 
	 	 SysPushConfig sysPushConfig=new SysPushConfig(callbackfun,  callbackmoudleid, description,  pushid);
	 		try {
				serviceSupportImp.save(sysPushConfig);
				getSession().removeAttribute(data_session_name);
				addPushConfigToSesson();
				writeResponseText("{success:true,msg:'success'}");
		 	} catch (Exception e) {
				writeResponseText("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
			  e.printStackTrace();
			}
	 }

	 
	 public void excutModifyQueryPushConfig()
		{
			System.out.println("excutModifyQuery...."+id);
			String hql="from SysPushConfig where 1=1 and id="+this.id; 
			
			List datas=serviceSupportImp.findByHQL(hql);
		 
		 	System.out.println("data size:"+datas.size());
		 	SysPushConfig sysPushConfig=(SysPushConfig)datas.get(0);
			try {
				System.out.println("{success:true,datas:"+JSONObject.fromObject(sysPushConfig).toString()+"}");
				writeResponseText("{success:true,datas:"+JSONObject.fromObject(sysPushConfig).toString()+"}");
		   	} catch (Exception e) {
				try {
					getResponse().getWriter().write("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
				} catch (IOException e1) {
				 
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
			
		}
		public void excuteModifyPushConfig()
		{
			System.out.println("excuteModify...."+id);
			String hql="from SysPushConfig where 1=1 and id="+this.id; 
			
			List datas=serviceSupportImp.findByHQL(hql);
		 
		 	SysPushConfig SysPushConfig=(SysPushConfig)datas.get(0);
		 
		 	SysPushConfig SysPushConfig2=new SysPushConfig(callbackfun,callbackmoudleid,description,pushid);
			String ignoreProperties[]={"id"};
		 	BeanUtils.copyProperties(SysPushConfig2, SysPushConfig,ignoreProperties);
		 	serviceSupportImp.update(SysPushConfig);
		 	getSession().removeAttribute(data_session_name);
		 	addPushConfigToSesson();
			writeResponseText("{success:true,msg:'success'}"); 
		}
		
		public void excuteDeletePushConfig()
		{
		  String hql="delete SysPushConfig where id in ("+deleterecord+")";
		  Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
		  session.beginTransaction();
		  session.createQuery(hql).executeUpdate();
		  session.getTransaction().commit();
		  addPushConfigToSesson();
		  writeResponseText("{success:true,msg:'success'}"); 
		}
		
		
	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getCallbackfun() {
		return callbackfun;
	}


	public void setCallbackfun(String callbackfun) {
		this.callbackfun = callbackfun;
	}


	public String getCallbackmoudleid() {
		return callbackmoudleid;
	}


	public void setCallbackmoudleid(String callbackmoudleid) {
		this.callbackmoudleid = callbackmoudleid;
	}


	 


	public String getPushid() {
		return pushid;
	}


	public void setPushid(String pushid) {
		this.pushid = pushid;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


 

 
	

}
